﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using Lists;
using Excel = Microsoft.Office.Interop.Excel;
using System.Collections;
using System.Data.SqlClient;
using GeneralLibs;


namespace ListForms
{
    public partial class FrmEditHangHoa : ListForms.FrmBaseEditForm
    {
        public FrmEditHangHoa(VatTuHangHoa VatTuHangHoa)
        {
            try
            {
                InitializeComponent();
                _DoiTuong = VatTuHangHoa;

                BasicForms.FrmBase.FormatTextEdit(txtGiaMua, BasicForms.TextFormatType.Money);
                BasicForms.FrmBase.FormatTextEdit(txtGiaBan, BasicForms.TextFormatType.Money);
                BasicForms.FrmBase.FormatTextEdit(txtGiaBia, BasicForms.TextFormatType.Money);
                BasicForms.FrmBase.FormatTextEdit(txtChietKhauBan, BasicForms.TextFormatType.Money);
                BasicForms.FrmBase.FormatTextEdit(txtChietKhauMua, BasicForms.TextFormatType.Money);
                BasicForms.FrmBase.FormatTextEdit(txtChietKhauBanLe, BasicForms.TextFormatType.Money);
            }
            catch (Exception ex)
            {
                BasicForms.FrmExceptionNotifier.ShowAndLog(ex);
            }
        }

        public FrmEditHangHoa(string ten)
        {
            try
            {
                InitializeComponent();
                _DoiTuong = null;
                txtTen.EditValue = ten;

                BasicForms.FrmBase.FormatTextEdit(txtGiaMua, BasicForms.TextFormatType.Money);
                BasicForms.FrmBase.FormatTextEdit(txtGiaBan, BasicForms.TextFormatType.Money);
                BasicForms.FrmBase.FormatTextEdit(txtGiaBia, BasicForms.TextFormatType.Money);
                BasicForms.FrmBase.FormatTextEdit(txtChietKhauBanLe, BasicForms.TextFormatType.Money);
                BasicForms.FrmBase.FormatTextEdit(txtChietKhauBan, BasicForms.TextFormatType.Money);
                BasicForms.FrmBase.FormatTextEdit(txtChietKhauMua, BasicForms.TextFormatType.Money);

                cmbNhomVatTuHangHoa.Focus();
            }
            catch (Exception ex)
            {
                BasicForms.FrmExceptionNotifier.ShowAndLog(ex);
            }
        }

        public FrmEditHangHoa(string ma, string ten)
        {
            try
            {
                InitializeComponent();
                _DoiTuong = null;
                txtMa.EditValue = ma;
                txtTen.EditValue = ten;

                BasicForms.FrmBase.FormatTextEdit(txtGiaMua, BasicForms.TextFormatType.Money);
                BasicForms.FrmBase.FormatTextEdit(txtGiaBan, BasicForms.TextFormatType.Money);
                BasicForms.FrmBase.FormatTextEdit(txtGiaBia, BasicForms.TextFormatType.Money);
                BasicForms.FrmBase.FormatTextEdit(txtChietKhauBanLe, BasicForms.TextFormatType.Money);
                BasicForms.FrmBase.FormatTextEdit(txtChietKhauBan, BasicForms.TextFormatType.Money);
                BasicForms.FrmBase.FormatTextEdit(txtChietKhauMua, BasicForms.TextFormatType.Money);

                cmbNhomVatTuHangHoa.Focus();
            }
            catch (Exception ex)
            {
                BasicForms.FrmExceptionNotifier.ShowAndLog(ex);
            }
        }

        protected override string GetMa()
        {
            return this.txtMa.Text.Trim();
        }
        protected override void GenNewBarCode(bool isNull)
        {
            try
            {
                if (isNull)
                {
                    txtMa.EditValue = DanhMucVatTuHangHoa.GenBarCode();
                }
                else
                {
                    txtMa.EditValue = null;
                }
            }
            catch (Exception ex)
            {
                BasicForms.FrmExceptionNotifier.ShowAndLog(ex);
            }
        }

        protected override void FillData()
        {
            cmbNhomVatTuHangHoa.DataSource = DanhMucNhomVatTuHangHoa.Filter("LienQuanDenKho = 1").DataView;
            cmbNhomVatTuHangHoa.DisplayColumn = "Ten";
            cmbNhomVatTuHangHoa.ValueColumn = "ID";
            cmbNhomVatTuHangHoa.AddVisibleColumn("Ma", "Mã");
            cmbNhomVatTuHangHoa.AddVisibleColumn("Ten", "Tên");

            if (_DoiTuong == null)
            {
                try
                {
                    cmbNhomVatTuHangHoa.Focus();
                    this.Text = "Thêm mới vật tư hàng hóa - dịch vụ";
                    _DoiTuong = DanhMucVatTuHangHoa.Instance.NewDoiTuong();
                    txtGiaMua.EditValue = (decimal)0;
                    txtGiaBan.EditValue = (decimal)0;
                    txtGiaBia.EditValue = (decimal)0;
                    txtChietKhauBanLe.EditValue = (int)0;
                    txtChietKhauBan.EditValue = (int)0;
                    txtChietKhauMua.EditValue = (int)0;

                    cmbTacGia.DataSource = DanhMucVatTuHangHoa.GetAllTacGia();
                    cmbTacGia.DisplayMember = "TacGia";
                    cmbTacGia.Text = "";

                    cmbNhaXuatBan.DataSource = DanhMucVatTuHangHoa.GetAllNhaXuatBan();
                    cmbNhaXuatBan.DisplayMember = "NhaXuatBan";
                    cmbNhaXuatBan.Text = "";

                    cmbNguonCungCap.DataSource = DanhMucVatTuHangHoa.GetAllNguonCungCap();
                    cmbNguonCungCap.DisplayMember = "NguonCungCap";
                    cmbNguonCungCap.Text = "";

                    cmbDonViTinh.DataSource = DanhMucVatTuHangHoa.GetAllDonViTinh();
                    cmbDonViTinh.DisplayMember = "DonViTinh";
                    cmbDonViTinh.Text = "";

                    _EditState = EditState.CreatingNew;
                }
                catch (SqlException sqlEx)
                {
                    throw new CustomException(CustomException.GetSqlExceptionMessage(sqlEx));
                }
                catch (Exception ex)
                {
                    if (ex is CustomException) throw ex;
                    else
                    {
                        throw new Exception(ex.Message);
                    }
                }
            }
            else
            {
                try
                {
                    cmbNhomVatTuHangHoa.Focus();

                    #region Doc du lieu
                    
                    this.Text = "Sửa thông tin vật tư hàng hóa - dịch vụ";
                    try
                    {
                        txtTen.Text = (_DoiTuong as VatTuHangHoa).Ten;
                    }
                    catch (Exception ex)
                    { }
                    try
                    {
                        txtTenNganGon.Text = (_DoiTuong as VatTuHangHoa).TenNganGon;
                    }
                    catch (Exception ex)
                    { }
                    try
                    {
                        cmbNhaXuatBan.Text = (_DoiTuong as VatTuHangHoa).NhaXuatBan;
                    }
                    catch (Exception ex)
                    { }
                    try
                    {
                        cmbNguonCungCap.Text = (_DoiTuong as VatTuHangHoa).NguonCungCap;
                    }
                    catch (Exception ex)
                    { }
                    try
                    {
                        cmbTacGia.Text = (_DoiTuong as VatTuHangHoa).TacGia;
                    }
                    catch (Exception ex)
                    { }
                    try
                    {
                        txtChietKhauBanLe.EditValue = (_DoiTuong as VatTuHangHoa).ChietKhauBanLe;
                    }
                    catch (Exception ex)
                    { }
                    try
                    {
                        txtChietKhauBan.EditValue = (_DoiTuong as VatTuHangHoa).ChietKhauBan;
                    }
                    catch (Exception ex)
                    { }
                    try
                    {
                        txtChietKhauMua.EditValue = (_DoiTuong as VatTuHangHoa).ChietKhauMua;
                    }
                    catch (Exception ex)
                    { }
                    try
                    {
                        rdoSuDung.Checked = (_DoiTuong as VatTuHangHoa).TrangThai;
                        rdoKhongSuDung.Checked = !(rdoSuDung.Checked);
                    }
                    catch (Exception ex)
                    { }
                    try
                    {
                        cmbNhomVatTuHangHoa.EditValue = (_DoiTuong as VatTuHangHoa).NhomVatTuHangHoa.ID;
                    }
                    catch (Exception ex)
                    { }
                    try
                    {
                        txtGiaBan.EditValue = (_DoiTuong as VatTuHangHoa).GiaBanHienTai;
                    }
                    catch (Exception ex)
                    { }
                    try
                    {
                        txtGiaMua.EditValue = (_DoiTuong as VatTuHangHoa).GiaMuaHienTai;
                    }
                    catch (Exception ex)
                    { }
                    try
                    {
                        txtMa.Text = (_DoiTuong as VatTuHangHoa).Ma;
                    }
                    catch (Exception ex)
                    { }
                    try
                    {
                        txtGiaBia.EditValue = (_DoiTuong as VatTuHangHoa).GiaBia;
                       
                    }
                    catch (Exception ex)
                    { }
                    try
                    {
                        txtGhiChu.EditValue = (_DoiTuong as VatTuHangHoa).GhiChu;
                    }
                    catch 
                    { }
                    try
                    {
                        txtKichThuoc.EditValue = (_DoiTuong as VatTuHangHoa).KichThuoc;
                    }
                    catch
                    { }
                    try
                    {
                        txtSoTrang.EditValue = (_DoiTuong as VatTuHangHoa).SoTrang;
                    }
                    catch
                    { }
                    #endregion

                    cmbTacGia.DataSource = DanhMucVatTuHangHoa.GetAllTacGia();
                    cmbTacGia.DisplayMember = "TacGia";
                    cmbTacGia.Text = (_DoiTuong as VatTuHangHoa).TacGia;

                    cmbNhaXuatBan.DataSource = DanhMucVatTuHangHoa.GetAllNhaXuatBan();
                    cmbNhaXuatBan.DisplayMember = "NhaXuatBan";
                    cmbNhaXuatBan.Text = (_DoiTuong as VatTuHangHoa).NhaXuatBan;

                    cmbNguonCungCap.DataSource = DanhMucVatTuHangHoa.GetAllNguonCungCap();
                    cmbNguonCungCap.DisplayMember = "NguonCungCap";
                    cmbNguonCungCap.Text = (_DoiTuong as VatTuHangHoa).NguonCungCap;

                    cmbDonViTinh.DataSource = DanhMucVatTuHangHoa.GetAllDonViTinh();
                    cmbDonViTinh.DisplayMember = "DonViTinh";
                    cmbDonViTinh.Text = (_DoiTuong as VatTuHangHoa).DonViTinh;

                    _EditState = EditState.Modifying;

                    txtMa.Properties.ReadOnly = VatTuHangHoa.DaDuocGiaoDich(_DoiTuong.ID) && 
                        (Config.DuocPhepSuaMaHangKhiDaGiaoDich == DuocPhepSuaMaHangKhiDaGiaoDichHayKhong.KhongDuocPhep);
                }
                catch (Exception ex)
                {
                }
            }
        }
        protected override bool CheckFullInfo()
        {
            if (txtMa.Text == "") throw new CustomException("Chưa điền mã vật tư hàng hóa - dịch vụ", ExceptionType.LackOfInputInfo);
            if (txtTen.Text == "") throw new CustomException("Chưa điền tên vật tư hàng hóa - dịch vụ", ExceptionType.LackOfInputInfo);
            if (cmbNhomVatTuHangHoa.EditValue == null) throw new CustomException("Chưa chọn nhóm vật tư hàng hóa - dịch vụ", ExceptionType.LackOfInputInfo);
            if (txtGiaBan.EditValue == null && txtGiaBan.Enabled) throw new CustomException("Chưa nhập giá bán mặc định", ExceptionType.LackOfInputInfo);
            if (txtGiaMua.EditValue == null && txtGiaMua.Enabled) throw new CustomException("Chưa nhập giá mua mặc định", ExceptionType.LackOfInputInfo);
            if (txtGiaBia.EditValue == null && txtGiaBia.Enabled) throw new CustomException("Chưa nhập giá bìa mặc định", ExceptionType.LackOfInputInfo);
            if (rdoSuDung.Checked == false && rdoKhongSuDung.Checked == false) throw new CustomException("Chưa chọn hàng sử dụng hay không sử dụng", ExceptionType.LackOfInputInfo);
            if (_EditState == EditState.Modifying && VatTuHangHoa.DaDuocGiaoDich(_DoiTuong.ID)
                && txtMa.Text != (_DoiTuong as VatTuHangHoa).Ma && Config.DuocPhepSuaMaHangKhiDaGiaoDich != DuocPhepSuaMaHangKhiDaGiaoDichHayKhong.DuocPhep)
            {
                throw new CustomException("Không được sửa mã của hàng đã được giao dịch");
            }
            return true;
        }

        protected override void SetDoiTuongProperties()
        {
            try
            {
                string maHang = txtMa.Text.Trim();
                string tenHang = txtTen.Text.Trim();
                
                if (_EditState == EditState.CreatingNew)
                {
                    VatTuHangHoa vthh = DanhMucVatTuHangHoa.Instance.GetByProperty("Ten", tenHang);
                    if (vthh != null)
                    {
                        //MessageBox.Show("Tên hàng này đã tồn tại trong danh mục với mã: " + vthh.Ma);
                        throw new CustomException("Tên hàng này đã tồn tại trong danh mục với mã: " + vthh.Ma);
                    }
                    else
                    {
                        vthh = DanhMucVatTuHangHoa.Instance.GetByProperty("Ma", maHang);
                        if (vthh != null)
                        {
                            //MessageBox.Show("Mã hàng này đã tồn tại trong danh mục với tên: " + vthh.Ten);
                            throw new CustomException("Mã hàng này đã tồn tại trong danh mục với tên: " + vthh.Ten);
                        }
                    }
                }
                
                #region Set value
                    //buoc phai co
                    (_DoiTuong as VatTuHangHoa).Ma = txtMa.Text.Trim();
                    (_DoiTuong as VatTuHangHoa).Ten = txtTen.Text.Trim();
                    (_DoiTuong as VatTuHangHoa).TrangThai = rdoSuDung.Checked;
                    (_DoiTuong as VatTuHangHoa).GiaBia = (decimal)txtGiaBia.EditValue;
                    (_DoiTuong as VatTuHangHoa).NhomVatTuHangHoa = DanhMucNhomVatTuHangHoa.Instance.GetByID((Guid)cmbNhomVatTuHangHoa.EditValue);
                    //khong dien cung duoc
                    if (txtTenNganGon.Enabled) (_DoiTuong as VatTuHangHoa).TenNganGon = txtTenNganGon.Text.Trim();
                    if (cmbTacGia.Enabled) (_DoiTuong as VatTuHangHoa).TacGia = cmbTacGia.Text.Trim();
                    if (cmbNguonCungCap.Enabled) (_DoiTuong as VatTuHangHoa).NguonCungCap = cmbNguonCungCap.Text.Trim();
                    if (cmbNhaXuatBan.Enabled) (_DoiTuong as VatTuHangHoa).NhaXuatBan = cmbNhaXuatBan.Text.Trim();
                    if (txtChietKhauBanLe.Enabled) (_DoiTuong as VatTuHangHoa).ChietKhauBanLe = Convert.ToDecimal(txtChietKhauBanLe.EditValue);
                    if (txtChietKhauBan.Enabled) (_DoiTuong as VatTuHangHoa).ChietKhauBan = Convert.ToDecimal(txtChietKhauBan.EditValue);
                    if (txtChietKhauMua.Enabled) (_DoiTuong as VatTuHangHoa).ChietKhauMua = Convert.ToDecimal(txtChietKhauMua.EditValue);
                    if (txtGiaBan.Enabled) (_DoiTuong as VatTuHangHoa).GiaBanHienTai = Convert.ToDecimal(txtGiaBan.EditValue);
                    else (_DoiTuong as VatTuHangHoa).GiaBanHienTai = 0;
                    if (txtGiaMua.Enabled) (_DoiTuong as VatTuHangHoa).GiaMuaHienTai = Convert.ToDecimal(txtGiaMua.EditValue);
                    else (_DoiTuong as VatTuHangHoa).GiaMuaHienTai = 0;
                    if (cmbDonViTinh.Enabled) (_DoiTuong as VatTuHangHoa).DonViTinh = cmbDonViTinh.Text.Trim();
                    if (txtGhiChu.Enabled) (_DoiTuong as VatTuHangHoa).GhiChu = txtGhiChu.Text.Trim();
                    if (txtKichThuoc.Enabled) (_DoiTuong as VatTuHangHoa).KichThuoc = txtKichThuoc.Text.Trim();
                    if (txtSoTrang.Enabled) (_DoiTuong as VatTuHangHoa).SoTrang = txtSoTrang.Text.Trim();
                    #endregion
            }
            catch (Exception ex)
            {
                BasicForms.FrmExceptionNotifier.ShowAndLog(ex);
            }
        }

        private void rdoSuDung_Click(object sender, EventArgs e)
        {
            rdoKhongSuDung.Checked = false; ;
        }

        private void rdoKhongSuDung_CheckedChanged(object sender, EventArgs e)
        {
            rdoSuDung.Checked = false;
        }

        private void txtGiaBan_EditValueChanged(object sender, EventArgs e)
        {
            txtGiaBia.EditValue = txtGiaBan.EditValue;
        }

        private void cmbNhomVatTuHangHoa_EditValueChanged(object sender, EventArgs e)
        {
            try
            {
                if (cmbNhomVatTuHangHoa.EditValue is Guid && (Guid)cmbNhomVatTuHangHoa.EditValue != Guid.Empty && 
                    (!VatTuHangHoa.DaDuocGiaoDich(_DoiTuong.ID) || Config.DuocPhepSuaMaHangKhiDaGiaoDich == DuocPhepSuaMaHangKhiDaGiaoDichHayKhong.DuocPhep))
                {
                    NhomVatTuHangHoa nhom = DanhMucNhomVatTuHangHoa.Instance.GetByID((Guid)cmbNhomVatTuHangHoa.EditValue);
                    if (nhom != null)
                    {
                        if (nhom.IsSach)
                        {
                            lblTitle.Text = "Sách";
                            lblGiaBia.Text = "Giá bán lẻ";
                            foreach (Control ctrl in this.Controls)
                            {
                                if (ctrl.Tag is string && ctrl.Tag.Equals("isSach")) ctrl.Enabled = true;
                            }
                        }
                        else
                        {
                            lblTitle.Text = "Hàng hóa khác sách";
                            lblGiaBia.Text = "Giá tại cửa hàng";
                            cmbNhaXuatBan.Text = "";
                            cmbTacGia.Text = "";
                            foreach (Control ctrl in this.Controls)
                            {
                                if (ctrl.Tag is string && ctrl.Tag.Equals("isSach")) ctrl.Enabled = false;
                            }
                        }
                    }
                    txtMa.Text = DanhMucVatTuHangHoa.GenBarCode();
                }
            }
            catch (Exception ex)
            {
                BasicForms.FrmExceptionNotifier.ShowAndLog(ex);
            }
        }

        private void txtGiaMua_EditValueChanged(object sender, EventArgs e)
        {
            try
            {
                decimal chietKhauTinhNguocTheoGiaNET = 0;
                decimal giaNET = 0;
                decimal giaBanLe = 0;
                if (_EditState == EditState.CreatingNew || _EditState == EditState.Modifying)
                {
                    if (txtGiaBia.EditValue != null && txtGiaMua.EditValue != null &&
                        (decimal)txtGiaBia.EditValue > 0 && (decimal)txtGiaMua.EditValue > 0)
                    {
                        giaNET = (decimal)txtGiaMua.EditValue;
                        giaBanLe = (decimal)txtGiaBia.EditValue;

                        if (giaBanLe != 0)
                        {
                            chietKhauTinhNguocTheoGiaNET = ((giaBanLe - giaNET) * 100) / giaBanLe;
                            txtChietKhauMua.EditValue = Math.Round(chietKhauTinhNguocTheoGiaNET, 1);
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                BasicForms.FrmExceptionNotifier.ShowAndLog(ex);
            }
        }

        private void txtGiaBia_EditValueChanged(object sender, EventArgs e)
        {
            try
            {
                decimal chietKhauTinhNguocTheoGiaNET = 0;
                decimal giaNET = 0;
                decimal giaBanLe = 0;
                if (_EditState == EditState.CreatingNew || _EditState == EditState.Modifying)
                {
                    if (txtGiaBia.EditValue != null && txtGiaMua.EditValue != null &&
                        (decimal)txtGiaBia.EditValue > 0 && (decimal)txtGiaMua.EditValue > 0)
                    {
                        giaNET = (decimal)txtGiaMua.EditValue;
                        giaBanLe = (decimal)txtGiaBia.EditValue;

                        if (giaBanLe != 0)
                        {
                            chietKhauTinhNguocTheoGiaNET = ((giaBanLe - giaNET) * 100) / giaBanLe;
                            txtChietKhauMua.EditValue = Math.Round(chietKhauTinhNguocTheoGiaNET, 1);
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                BasicForms.FrmExceptionNotifier.ShowAndLog(ex);
            }
        }

        private void FrmEditHangHoa_Load(object sender, EventArgs e)
        {
            if (Config.DuocPhepSuaDanhMuc == DuocPhepSuaDanhMucHayKhong.KhongDuocPhep)
            {
                cmbNhomVatTuHangHoa.popupList.Properties.ReadOnly = true;
                txtMa.Properties.ReadOnly = true;
                txtTen.Properties.ReadOnly = true;
                txtTenNganGon.Properties.ReadOnly = true;
                txtGiaMua.Properties.ReadOnly = true;
                txtGiaBan.Properties.ReadOnly = true;
                txtGiaBia.Properties.ReadOnly = true;
                cmbNhaXuatBan.Enabled = false;
                cmbNguonCungCap.Enabled = false;
                cmbTacGia.Enabled = false;
                txtChietKhauBanLe.Properties.ReadOnly = true;
                txtChietKhauBan.Properties.ReadOnly = true;
                txtChietKhauMua.Properties.ReadOnly = true;
                cmbDonViTinh.Enabled = false;
                txtKichThuoc.Properties.ReadOnly = true;
                txtSoTrang.Properties.ReadOnly = true;
                txtGhiChu.Properties.ReadOnly = true;
            }
        }
    }
}
